#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
    if(this->my_thread!=nullptr)
    {
        this->my_task->stopWork();
        this->my_thread->quit();
        this->my_thread->wait();
        delete this->my_thread;
    }
}


void Widget::on_pushButton_clicked()
{
    if(this->my_thread!=nullptr)
    {
        return;
    }
    this->my_thread=new QThread;
    this->my_task=new task;
    this->my_task->moveToThread(this->my_thread);
    connect(this->my_thread,&QThread::started,this->my_task,&task::work);
    connect(this->my_thread,&QThread::finished,this->my_task,&task::deleteLater);
    connect(this->my_task,&task::upUi,this,&Widget::on_setText);
    this->my_thread->start();
}

void Widget::on_setText()
{
    ui->textEdit->append("hello world");
    qDebug()<<"添加成功";
}
